<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="提现编号" prop="withdrawSn">
        <el-input
          v-model="queryParams.withdrawSn"
          placeholder="请输入编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提现状态" prop="withdrawState">
        <el-select v-model="queryParams.withdrawState" placeholder="请选择提现状态" clearable>
          <el-option
            v-for="dict in dict.type.uac_member_withdraw_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="auditState">
        <el-select v-model="queryParams.auditState" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.uac_member_withdraw_audit_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核人员" prop="auditorId">
        <el-input
          v-model="queryParams.auditorId"
          placeholder="请输入审核人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="付款状态" prop="payState">
        <el-select v-model="queryParams.payState" placeholder="请选择付款状态" clearable>
          <el-option
            v-for="dict in dict.type.uac_member_withdraw_pay_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付方式" prop="payMethod">
        <el-select v-model="queryParams.payMethod" placeholder="请选择支付方式" clearable>
          <el-option
            v-for="dict in dict.type.uac_member_withdraw_pay_method"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['member:memberWithdraw:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="withdrawList" @selection-change="handleSelectionChange">
      <el-table-column label="会员名称" align="center" prop="memberName"/>
      <el-table-column label="编号" align="center" prop="withdrawSn"/>
      <el-table-column label="提现金额" align="center" prop="withdrawAmount"/>
      <el-table-column label="实际金额" align="center" prop="realAmount"/>
      <el-table-column label="手续费" align="center" prop="fee"/>

      <el-table-column label="审核状态" align="center" prop="auditState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.uac_member_withdraw_audit_state" :value="scope.row.auditState"/>
        </template>
      </el-table-column>
      <el-table-column label="提现状态" align="center" prop="withdrawState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.uac_member_withdraw_state" :value="scope.row.withdrawState"/>
        </template>
      </el-table-column>
      <el-table-column label="付款状态" align="center" prop="payState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.uac_member_withdraw_pay_state" :value="scope.row.payState"/>
        </template>
      </el-table-column>
      <el-table-column label="审核备注" align="center" prop="auditRemark"/>
      <el-table-column label="审核时间" align="center" prop="auditTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="auditor"/>
      <el-table-column label="付款备注" align="center" prop="payRemark"/>
      <el-table-column label="支付方式" align="center" prop="payMethod">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.uac_member_withdraw_pay_method" :value="scope.row.payMethod"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleAudit(scope.row)"
            v-if="scope.row.auditState == 0"
            v-hasPermi="['member:memberWithdraw:audit']"
          >审核
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['member:memberWithdraw:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员提现对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="会员ID" prop="memberId">
          <el-input v-model="form.memberId" placeholder="请输入会员ID"/>
        </el-form-item>
        <el-form-item label="会员名称" prop="memberName">
          <el-input v-model="form.memberName" placeholder="请输入会员名称"/>
        </el-form-item>
        <el-form-item label="编号" prop="withdrawSn">
          <el-input v-model="form.withdrawSn" placeholder="请输入编号"/>
        </el-form-item>
        <el-form-item label="申请提现金额" prop="withdrawAmount">
          <el-input v-model="form.withdrawAmount" placeholder="请输入申请提现金额"/>
        </el-form-item>
        <el-form-item label="实际金额 (申请提现金额  -  体现手续费)" prop="realAmount">
          <el-input v-model="form.realAmount" placeholder="请输入实际金额 (申请提现金额  -  体现手续费)"/>
        </el-form-item>
        <el-form-item label="提现手续费" prop="fee">
          <el-input v-model="form.fee" placeholder="请输入提现手续费"/>
        </el-form-item>
        <el-form-item label="提现状态  0 等待打款  1 打款中 2 提现成功 -1 提现失败  " prop="withdrawState">
          <el-select v-model="form.withdrawState"
                     placeholder="请选择提现状态  0 等待打款  1 打款中 2 提现成功 -1 提现失败  ">
            <el-option
              v-for="dict in dict.type.uac_member_withdraw_state"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核状态  0 待审核  1 审核通过   -1 审核拒绝" prop="auditState">
          <el-select v-model="form.auditState" placeholder="请选择审核状态  0 待审核  1 审核通过   -1 审核拒绝">
            <el-option
              v-for="dict in dict.type.uac_member_withdraw_audit_state"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核备注" prop="auditRemark">
          <el-input v-model="form.auditRemark" placeholder="请输入审核备注"/>
        </el-form-item>
        <el-form-item label="审核时间" prop="auditTime">
          <el-date-picker clearable
                          v-model="form.auditTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择审核时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核人员姓名" prop="auditor">
          <el-input v-model="form.auditor" placeholder="请输入审核人员姓名"/>
        </el-form-item>
        <el-form-item label="审核人员ID" prop="auditorId">
          <el-input v-model="form.auditorId" placeholder="请输入审核人员ID"/>
        </el-form-item>
        <el-form-item label="付款状态  0 待付款  1  付款中   2  付款成功   -1 付款失败" prop="payState">
          <el-select v-model="form.payState"
                     placeholder="请选择付款状态  0 待付款  1  付款中   2  付款成功   -1 付款失败">
            <el-option
              v-for="dict in dict.type.uac_member_withdraw_pay_state"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="付款备注" prop="payRemark">
          <el-input v-model="form.payRemark" placeholder="请输入付款备注"/>
        </el-form-item>
        <el-form-item label="支付方式   online 线上打款   offline 线下打款" prop="payMethod">
          <el-select v-model="form.payMethod" placeholder="请选择支付方式   online 线上打款   offline 线下打款">
            <el-option
              v-for="dict in dict.type.uac_member_withdraw_pay_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="凭据" prop="receipts">
          <el-input v-model="form.receipts" placeholder="请输入凭据"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog title="提现审核" :visible.sync="auditOpen" v-if="auditOpen" width="500px" append-to-body>
      <el-form ref="auditForm" :model="auditForm" :rules="auditRules" label-width="80px">
        <el-form-item label="审核结果" prop="auditState">
          <el-select v-model="auditForm.auditState" placeholder="请选择审核结果">
            <el-option
              v-for="dict in dict.type.uac_member_withdraw_audit_state"
              :key="dict.value"
              :label="dict.label"
              :disabled="dict.value == 0"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核备注" prop="auditRemark">
          <el-input v-model="auditForm.auditRemark" placeholder="请输入审核备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAuditForm">确 定</el-button>
        <el-button @click="auditOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listWithdraw, getWithdraw, delWithdraw, addWithdraw, updateWithdraw, auditWithdraw} from "@/api/uac/withdraw";

export default {
  name: "MemberWithdraw",
  dicts: ['uac_member_withdraw_pay_state', 'uac_member_withdraw_audit_state', 'uac_member_withdraw_state','uac_member_withdraw_pay_method'],
  props: {
    memberId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员提现表格数据
      withdrawList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      auditOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        memberId: null,
        memberName: null,
        withdrawSn: null,
        withdrawAmount: null,
        realAmount: null,
        fee: null,
        withdrawState: null,
        auditState: null,
        auditRemark: null,
        auditTime: null,
        auditor: null,
        auditorId: null,
        payState: null,
        payRemark: null,
        payMethod: null,
        receipts: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      auditRules: {
        auditState: [
          {required: true, message: "请选择审核结果", trigger: "blur"},
        ],
        auditRemark: [
          {required: true, message: "请填写审核意见", trigger: "blur"},
        ]
      },
      auditForm: {
        id: null,
        auditState: null,
        auditRemark: ""
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询会员提现列表 */
    getList() {
      this.loading = true;
      listWithdraw(this.queryParams).then(response => {
        this.withdrawList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        memberId: null,
        memberName: null,
        withdrawSn: null,
        withdrawAmount: null,
        realAmount: null,
        fee: null,
        withdrawState: null,
        auditState: null,
        auditRemark: null,
        auditTime: null,
        auditor: null,
        auditorId: null,
        payState: null,
        payRemark: null,
        payMethod: null,
        receipts: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员提现";
    },
    /** 审核按钮操作 */
    handleAudit(row) {
      const id = row.id || this.ids
      this.auditOpen = true;
      this.auditForm.id = id
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWithdraw(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWithdraw(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 审核提交按钮 */
    submitAuditForm() {
      this.$refs["auditForm"].validate(valid => {
        if (valid) {
          auditWithdraw(this.auditForm).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.auditOpen = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除会员提现编号为"' + ids + '"的数据项？').then(function () {
        return delWithdraw(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('member/withdraw/export', {
        ...this.queryParams
      }, `withdraw_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
